package com.sky.controller.user;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.netty.channel.AddressedEnvelope;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Slf4j
@RequestMapping("/user/addressBook")
@Api(tags = "c端地址簿相关接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @return addressBook
     */
    @ApiOperation("新增地址")
    @PostMapping
    public Result addAddressBook(@RequestBody AddressBook addressBook){
        log.info("新增地址：{}",addressBook);
        addressBookService.addAddressBook(addressBook);
        return Result.success();
    }

    /**
     * 查询当前登录用户的所有地址信息
     * @return
     */
    @ApiOperation("查询当前登录用户的所有地址信息")
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        log.info("查询所有地址信息");
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(addressBook);
        return Result.success(list);
    }

    /**
     * 查询默认地址
     * @return
     */
    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getDefault(){
        //SQL:select * from address_book where user_id = ? and is_default = 1
        log.info("查询默认地址信息");
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(1);
        addressBook.setUserId(BaseContext.getCurrentId());

        List<AddressBook> list = addressBookService.list(addressBook);
        if (list != null && list.size() == 1) {
            return Result.success(list.get(0));
        }
        return Result.error(MessageConstant.NoGetCurrentAddressBook);
    }

    /**
     * 根据id修改地址信息
     * @param addressBook
     * @return
     */
    @ApiOperation("根据id修改地址信息")
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        log.info("根据id修改地址信息");
        addressBookService.updateAddressBook(addressBook);
        return Result.success();
    }

    /**
     * 根据id查看地址信息 - 修改地址时回显数据
     * @param id
     * @return
     */
    @ApiOperation("根据id查询地址信息")
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id){
        log.info("根据id查询地址信息");
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 删除地址信息
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除地址信息")
    public Result deleteById(Long id){
        log.info("删除地址信息: " + id);
        addressBookService.deleteById(id);
        return Result.success();
    }

    /**
     * 设置默认地址信息
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址信息")
    public Result setDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认地址信息: {}", addressBook);
        addressBookService.setDefault(addressBook);
        return Result.success();
    }
}
